package com.cjk.console.ctrl.sys;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cjk.console.request.RoleRequest;
import com.cjk.console.service.AuthService;
import com.cjk.foundation.commons.BusinessException;
import com.cjk.foundation.commons.response.PageResponse;
import com.cjk.foundation.commons.response.Response;
import com.cjk.foundation.dao.RoleDao;

@Controller
@RequestMapping("/role")
public class RoleController {
	
	@Autowired
	private RoleDao roleDao;
	
	@Autowired
	private AuthService authService;

	@RequestMapping(value = "/getRoleList.do", method = RequestMethod.POST)
	@ResponseBody
	public PageResponse getRoleList(Integer page, Integer pageSize){
		return authService.getRoleList(page, pageSize);
	}
	
	@RequestMapping(value = "/getRoleInfo.do", method = RequestMethod.POST)
	@ResponseBody
	public Response getRoleInfo(Integer id){
		return Response.success(roleDao.selectRoleName(id));
	}
	
	@RequestMapping(value = "/saveRole.do", method = RequestMethod.POST)
	@ResponseBody
	public Response saveRole(@RequestBody RoleRequest request){
		authService.saveRole(request);
		return Response.success();
	}
	
	@RequestMapping(value = "/delRole.do", method = RequestMethod.POST)
	@ResponseBody
	public Response delRole(Integer id){
		Integer count = roleDao.countRoleInUsed(id);
		if(count != 0){
			throw new BusinessException("删除的角色当前应用于账号不可删除");
		}
		authService.deleteRole(id);
		return Response.success();
	}
}
